package com.tpl.core.basedata.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.tpl.core.basedata.dao.DeptDao;
import com.tpl.core.basedata.service.DeptService;
import com.tpl.core.common.exception.RuleException;
import com.tpl.core.common.page.ListPage;
import com.tpl.core.common.page.Page;
import com.tpl.core.dao.ExecuteSql;
import com.tpl.core.entity.sys.Dept;
import com.tpl.core.entity.sys.User;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;



/**
 * 说明 :部门管理业务处理类
 * @author <a href="mailto:3380084@gmail.com">张坤雷</a>
 * @version 创建时间：2013-7-2 下午5:14:33 
 *
 */
public class DeptServiceImpl implements DeptService {
	@Autowired
	private DeptDao deptDao;

	@Override
	public List<Dept> getDepts(Long userId, Long parentId) {
		return getDepts(userId, parentId, null);
	}
	@Override
	public List<Dept> getDepts(Long userId, Long parentId, Long orgId) {
		List<Dept> list=deptDao.getDepts(userId, parentId,orgId);
		return list;
	}


	@Override
	public void doSave(Dept dept) {
		String preLable="<font color=\"red\" style=\"font-weight: bold;\">";
		String endLable="</font>";
		boolean flag=false;
		//如果是增加，判断部门编号是否存在,存在则flag=true
				if(dept.getId()==null){
					/*flag=checkDeptNo(dept.getDeptNo(),null);*/
					flag=addcheckDeptNo(dept.getType(), dept.getDeptNo(), dept.getId(),dept.getParentId());
						if(flag){
							throw new RuleException(preLable+"部门编号"+endLable+"部门编号已存在");
						}
				}//如果是更新，判断其他部门中是否存在该部门要修改的编号，存在则flag=true
				else{
					Dept odept=getDeptById(dept.getId());
					if(Boolean.FALSE.equals(odept.getDeleted())){
						flag=addcheckDeptNo(dept.getType(),dept.getDeptNo(),dept.getId(),dept.getParentId());
						if(flag){
							throw new RuleException(preLable+"部门编号"+endLable+"其他部门中已存在此编号");
						}
					}
				}
				//Dept pdept=getDeptById(dept.getParentId());
				
//				if(pdept!=null&&pdept.getType()!=1&&pdept.getType()!=dept.getType()){
//					throw new RuleException(preLable+"部门类型"+endLable+"请选择和上级部门相同的部门类型");
//				}
		if(dept.getId()!=null){
			deptDao.updateDept(dept);
		}
		else{
			dept.setCreateDate(dept.getUpdateDate());
			dept.setCreator(dept.getUpdator());
			deptDao.insertDept(dept);
//			User u=SessionUtils.getLoginUser();
//			if(!u.isAdmin())
//				ExecuteSql.execute("Insert Into Sys_Dept_Re_User(Dept_Id,User_Id,Default_Flag,State,User_Type)" +
//						"values(?,?,0,1,2)", dept.getId(),u.getId());
			
		}
	}

	@Override
	public void doDelete(Long id) {
		int c=ExecuteSql.getSingleResult(Integer.class, "select count(1) from SYS_DEPT_RE_USER where DEPT_ID=?", id);
		if(c>0)throw new RuleException("该部门下有人员,不能执行删除,请先清空人员");
		deptDao.deleteDept(id);
	}
	
	@Override
	public void delTxls(Long[] delTxl) {
		if(delTxl.length>0){
			for(int i = 0; i < delTxl.length; i++){
				ExecuteSql.execute("delete from sm_address_book where id=?",delTxl[i]);
			}
		}
		
	}

	/*@Override
	public boolean checkDeptNo(String deptNo, Long id) {
		int count=deptDao.checkDeptNo(deptNo, id);
		if(count>0){
			return true;
		}
			return false;
	}*/
	
	@Override
	public boolean addcheckDeptNo(int type, String deptNo, Long id, Long parentId) {
		// TODO Auto-generated method stub
		int count=deptDao.addcheckDeptNo(type, deptNo, id, parentId);
				if(count>0){
					return true;
				}
				    return false;
		
	}


	@Override
	public Dept getDeptById(Long id) {
		Dept dept=deptDao.getDeptById(id);
		return dept;
	}
	@Override
	public List<Object[]> getDeptNames(Integer type) {
		String sql ="";
		List<Object[]> deptNameList=null;
		if(type!=null){
			sql= "select d.id,d.dept_name deptName from sys_dept d where d.type=? and d.deleted=0 order by SORT_NUM";
			deptNameList= ExecuteSql.getFields(sql,type);
		}else{
			sql= "select d.id,d.dept_name deptName from sys_dept d where d.deleted=0 order by SORT_NUM";
			deptNameList=ExecuteSql.getFields(sql);
		}
		return deptNameList;
	}


	@Override
	public List<Dept> getDeptsByParentId(Long parentId) {
		return deptDao.getDeptsByParentId(parentId);
	}


	@Override
	public List<Dept> getDeptsByType(String type) {
		return deptDao.getDeptByType(type);
	}
	@Override
	public List<JSONObject> getAddressBook(Long deptId) {
		String sql = "select * from sm_address_book where dept_id=?";
		return ExecuteSql.getAlls(sql, deptId);
	}
	@Override
	public Page getDeptByDeptIds(String deptIds, int pageIndex, int pageSize) {
		int count=deptDao.getDeptByDeptIdsCount(deptIds);
		List<User> list=null;
		if(count>0){
			list= deptDao.getDeptByDeptIds(deptIds,pageIndex,pageSize);
			return new ListPage(list, pageIndex, pageSize, count);
		}
		return new ListPage(new ArrayList<>(),count);
	}
	@Override
	public List<Dept> getDeptByIds(String deptIds) {
		return deptDao.getDeptByIds(deptIds);
	}
	@Override
	public Dept getDeptByDeptNo(String deptNo, Long centerId) {
		return deptDao.getDeptByDeptNo(deptNo,centerId);
	}
	/*@Override
	public int addcheckDeptNo(String deptNo, Long id) {
		return deptDao.addcheckDeptNo(deptNo, id);
	}*/
	@Override
	public List<Dept> getAllDept(String deptName) {
		// TODO Auto-generated method stub
		return deptDao.getAllDept(deptName);
	}
	
	

}
